﻿#include "plcdlg.h"
#include "ui_plcdlg.h"
#include <QDebug>
PlcDlg::PlcDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PlcDlg)
{
    ui->setupUi(this);
    m_tPlc = new PlcThread();
}

PlcDlg::~PlcDlg()
{
    delete ui;
}

void PlcDlg::InitTimer()
{

}

void PlcDlg::GetPlcInfo()
{

}

void PlcDlg::slotReadInfo(PlcInfo plcInfo)
{
}


void PlcDlg::TimerTimeOut()
{

}

void PlcDlg::Start()
{
    // 停止定时器

    // 启动PLC
    m_tPlc->m_worker->writePlcInfo(START, 1);
    m_tPlc->m_worker->writePlcInfo(STOP, 0);

}

void PlcDlg::Stop()
{

    // 启动定时器
    // 停止PLC
    m_tPlc->m_worker->writePlcInfo(START, 0);
    m_tPlc->m_worker->writePlcInfo(STOP, 1);
}


void PlcDlg::on_inBT_1_clicked()
{

}
void PlcDlg::on_inBT_2_clicked()
{
}

void PlcDlg::on_confirmBt_1_clicked()
{
    // 大盘机转速
    m_tPlc->m_worker->writePlcInfo(MOTOR1_REV, ui->inEdit_1->text().toInt());
}
void PlcDlg::on_confirmBt_2_clicked()
{
    // 相机1距离
    m_tPlc->m_worker->writePlcInfo(PCCD_1, ui->inEdit_2->text().toInt());
}

void PlcDlg::on_confirmBt_3_clicked()
{
    // 相机2距离
    m_tPlc->m_worker->writePlcInfo(PCCD_2, ui->inEdit_3->text().toInt());
}

void PlcDlg::on_confirmBt_4_clicked()
{
    // 相机3距离
    m_tPlc->m_worker->writePlcInfo(PCCD_3, ui->inEdit_4->text().toInt());
}

void PlcDlg::on_confirmBt_5_clicked()
{
    // 相机4距离
    m_tPlc->m_worker->writePlcInfo(PCCD_4, ui->inEdit_5->text().toInt());

}

void PlcDlg::on_confirmBt_6_clicked()
{
    // 相机5距离
    m_tPlc->m_worker->writePlcInfo(PCCD_5, ui->inEdit_6->text().toInt());
}

void PlcDlg::on_confirmBt_7_clicked()
{
    //  相机6距离
    m_tPlc->m_worker->writePlcInfo(PCCD_6, ui->inEdit_7->text().toInt());
}

void PlcDlg::on_confirmBt_19_clicked()
{
    //  相机7距离
    m_tPlc->m_worker->writePlcInfo(PCCD_7, ui->inEdit_19->text().toInt());
}

void PlcDlg::on_confirmBt_20_clicked()
{
    //  相机8距离
    m_tPlc->m_worker->writePlcInfo(PCCD_8, ui->inEdit_20->text().toInt());
}

void PlcDlg::on_confirmBt_9_clicked()
{
    //    电磁阀距离1
    m_tPlc->m_worker->writePlcInfo(MOTOR_NG1, ui->inEdit_9->text().toInt());
}

void PlcDlg::on_confirmBt_10_clicked()
{
    //    电磁阀距离2
    m_tPlc->m_worker->writePlcInfo(MOTOR_NG2, ui->inEdit_10->text().toInt());
}

void PlcDlg::on_confirmBt_11_clicked()
{
    //     PC_电磁阀3距离
    m_tPlc->m_worker->writePlcInfo(MOTOR_NG3, ui->inEdit_11->text().toInt());
}
void PlcDlg::on_confirmBt_12_clicked()
{
    //    PC_电磁阀4距离
    m_tPlc->m_worker->writePlcInfo(MOTOR_NG4, ui->inEdit_12->text().toInt());
}

void PlcDlg::on_confirmBt_13_clicked()
{
    //    PC_电磁阀5距离
    m_tPlc->m_worker->writePlcInfo(MOTOR_NG5, ui->inEdit_13->text().toInt());
}

void PlcDlg::on_confirmBt_14_clicked()
{
    //    PC_电磁阀6距离
    m_tPlc->m_worker->writePlcInfo(MOTOR_NG6, ui->inEdit_14->text().toInt());

}
void PlcDlg::on_inBT_4_clicked()
{
    //    玻璃盘电机脉冲
    m_nInBt4 = 1 - m_nInBt4;
    m_tPlc->m_worker->writePlcInfo(QPULSE1, m_nInBt4);

}

void PlcDlg::on_inBT_5_clicked()
{
    //    玻璃盘电机方向
    m_nInBt5 = 1 - m_nInBt5;
    m_tPlc->m_worker->writePlcInfo(QDIRECTION1, m_nInBt5);
}

void PlcDlg::on_inBT_6_clicked()
{
    //    玻璃盘电机使能
    m_nInBt6 = 1 - m_nInBt6;
    m_tPlc->m_worker->writePlcInfo(QENABLED1, m_nInBt6);
}

void PlcDlg::on_inBT_10_clicked()
{
    //    相机1拍照
    m_nInBt10 = 1 - m_nInBt10;
    m_tPlc->m_worker->writePlcInfo(QGRAP1, m_nInBt10);
}

void PlcDlg::on_inBT_11_clicked()
{
    //    相机2拍照
    m_nInBt11 = 1 - m_nInBt11;
    m_tPlc->m_worker->writePlcInfo(QGRAP2, m_nInBt11);
}

void PlcDlg::on_inBT_12_clicked()
{
    //    相机3拍照
    m_nInBt12 = 1 - m_nInBt12;
    m_tPlc->m_worker->writePlcInfo(QGRAP3, m_nInBt12);
}

void PlcDlg::on_inBT_13_clicked()
{
    //    相机4拍照
    m_nInBt13 = 1 - m_nInBt13;
    m_tPlc->m_worker->writePlcInfo(QGRAP4, m_nInBt13);
}

void PlcDlg::on_inBT_14_clicked()
{
    //    相机5拍照
        m_nInBt14 = 1 - m_nInBt14;
        m_tPlc->m_worker->writePlcInfo(QGRAP5, m_nInBt14);
}

void PlcDlg::on_inBT_15_clicked()
{
    //    相机6拍照
        m_nInBt15 = 1 - m_nInBt15;
        m_tPlc->m_worker->writePlcInfo(QGRAP6, m_nInBt15);
}

void PlcDlg::on_inBT_34_clicked()
{
    // 相机7拍照
    m_nInBt34 = 1 - m_nInBt34;
    m_tPlc->m_worker->writePlcInfo(QGRAP7, m_nInBt34);
}

void PlcDlg::on_inBT_35_clicked()
{
    // 相机8拍照
    m_nInBt35 = 1 - m_nInBt35;
    m_tPlc->m_worker->writePlcInfo(QGRAP8, m_nInBt35);
}

void PlcDlg::on_inBT_19_clicked()
{
    //    电磁阀1
    m_nInBt19 = 1 - m_nInBt19;
    m_tPlc->m_worker->writePlcInfo(QNG1OUTAIR, m_nInBt19);
}

void PlcDlg::on_inBT_20_clicked()
{
    //    电磁阀2
    m_nInBt20 = 1 - m_nInBt20;
    m_tPlc->m_worker->writePlcInfo(QNG2OUTAIR, m_nInBt20);
}

void PlcDlg::on_inBT_21_clicked()
{
    //    电磁阀3
    m_nInBt21 = 1 - m_nInBt21;
    m_tPlc->m_worker->writePlcInfo(QNG3OUTAIR, m_nInBt21);
}

void PlcDlg::on_inBT_22_clicked()
{
    //    电磁阀4
    m_nInBt22 = 1 - m_nInBt22;
    m_tPlc->m_worker->writePlcInfo(QNG4OUTAIR, m_nInBt22);
}

void PlcDlg::on_inBT_23_clicked()
{
    //    电磁阀5
    m_nInBt23 = 1 - m_nInBt23;
    m_tPlc->m_worker->writePlcInfo(QNG5OUTAIR, m_nInBt23);

}

void PlcDlg::on_inBT_24_clicked()
{
    //    电磁阀6
    m_nInBt24 = 1 - m_nInBt24;
    m_tPlc->m_worker->writePlcInfo(QNG6OUTAIR, m_nInBt24);

}

void PlcDlg::on_inBT_7_clicked()
{
    //    震动盘
    m_nInBt7 = 1 - m_nInBt7;
    m_tPlc->m_worker->writePlcInfo(QPLATE, m_nInBt7);

}

void PlcDlg::on_inBT_16_clicked()
{
    //    系统上气
    m_nInBt16 = 1 - m_nInBt16;
    m_tPlc->m_worker->writePlcInfo(QINAIR, m_nInBt16);

}

void PlcDlg::on_inBT_25_clicked()
{
    //    照明灯
    m_nInBt25 = 1 - m_nInBt25;
    m_tPlc->m_worker->writePlcInfo(QLINE, m_nInBt25);

}

void PlcDlg::on_inBT_26_clicked()
{
    //    暂停灯
    m_nInBt26 = 1 - m_nInBt26;
    m_tPlc->m_worker->writePlcInfo(QTIMEOUT_LINE, m_nInBt26);

}

void PlcDlg::on_inBT_8_clicked()
{
    //    三色灯-红
    m_nInBt8 = 1 - m_nInBt8;
    m_tPlc->m_worker->writePlcInfo(QLIGHTR, m_nInBt8);

}

void PlcDlg::on_inBT_9_clicked()
{
    //    三色灯-黄
    m_nInBt9 = 1 - m_nInBt9;
    m_tPlc->m_worker->writePlcInfo(QLIGHTY, m_nInBt9);

}

void PlcDlg::on_inBT_17_clicked()
{
    //    三色灯-绿
    m_nInBt17 = 1 - m_nInBt17;
    m_tPlc->m_worker->writePlcInfo(QLIGHTG, m_nInBt17);

}

void PlcDlg::on_inBT_18_clicked()
{
    //    三色灯-鸣
    m_nInBt18 = 1 - m_nInBt18;
    m_tPlc->m_worker->writePlcInfo(QLIGHTVOX, m_nInBt18);
}





void PlcDlg::on_outBBt_17_clicked()
{
    //    get 编码器值
    m_tPlc->m_worker->readPlcInfo(ENCODER);
    ui->outBBt_17->setText(QString::number(m_tPlc->m_worker->m_nENCODER));
}

void PlcDlg::on_outBBt_8_clicked()
{
    //    get 大盘转速
    m_tPlc->m_worker->readPlcInfo(MOTOR1_REV);
    ui->outBBt_8->setText(QString::number(m_tPlc->m_worker->m_nMOTOR1_REV));
}



void PlcDlg::on_outBBt_2_clicked()
{
    // get 相机1距离
    m_tPlc->m_worker->readPlcInfo(PCCD_1);
    ui->outBBt_2->setText(QString::number(m_tPlc->m_worker->m_nPCCD_1));
}

void PlcDlg::on_outBBt_3_clicked()
{
    // get 相机2距离
    m_tPlc->m_worker->readPlcInfo(PCCD_2);
    ui->outBBt_3->setText(QString::number(m_tPlc->m_worker->m_nPCCD_2));
}

void PlcDlg::on_outBBt_4_clicked()
{
    // get 相机3距离
    m_tPlc->m_worker->readPlcInfo(PCCD_3);
    ui->outBBt_4->setText(QString::number(m_tPlc->m_worker->m_nPCCD_3));
}

void PlcDlg::on_outBBt_5_clicked()
{
    // get 相机4距离
    m_tPlc->m_worker->readPlcInfo(PCCD_4);
    ui->outBBt_5->setText(QString::number(m_tPlc->m_worker->m_nPCCD_4));
}

void PlcDlg::on_outBBt_6_clicked()
{
    // get 相机5距离
    m_tPlc->m_worker->readPlcInfo(PCCD_5);
    ui->outBBt_6->setText(QString::number(m_tPlc->m_worker->m_nPCCD_5));
}

void PlcDlg::on_outBBt_7_clicked()
{
    // get 相机6距离
    m_tPlc->m_worker->readPlcInfo(PCCD_6);
    ui->outBBt_7->setText(QString::number(m_tPlc->m_worker->m_nPCCD_6));
}

void PlcDlg::on_outBBt_9_clicked()
{
    // get 相机7的距离
    m_tPlc->m_worker->readPlcInfo(PCCD_7);
    ui->outBBt_9->setText(QString::number(m_tPlc->m_worker->m_nPCCD_7));
}

void PlcDlg::on_outBBt_22_clicked()
{
    // get 相机8的距离
    m_tPlc->m_worker->readPlcInfo(PCCD_8);
    ui->outBBt_22->setText(QString::number(m_tPlc->m_worker->m_nPCCD_8));
}

void PlcDlg::on_outBBt_10_clicked()
{
    // get 真空距离
    m_tPlc->m_worker->readPlcInfo(MOTOR_NG1);
    ui->outBBt_10->setText(QString::number(m_tPlc->m_worker->m_nMOTOR_NG1));
}

void PlcDlg::on_outBBt_11_clicked()
{
    // get 倒料距离
    m_tPlc->m_worker->readPlcInfo(MOTOR_NG2);
    ui->outBBt_11->setText(QString::number(m_tPlc->m_worker->m_nMOTOR_NG2));
}

void PlcDlg::on_outBBt_12_clicked()
{
    // get NG1距离
    m_tPlc->m_worker->readPlcInfo(MOTOR_NG3);
    ui->outBBt_12->setText(QString::number(m_tPlc->m_worker->m_nMOTOR_NG3));
}

void PlcDlg::on_outBBt_13_clicked()
{
    // get NG2距离
    m_tPlc->m_worker->readPlcInfo(MOTOR_NG4);
    ui->outBBt_13->setText(QString::number(m_tPlc->m_worker->m_nMOTOR_NG4));
}

void PlcDlg::on_outBBt_14_clicked()
{
    // get NG3距离
    m_tPlc->m_worker->readPlcInfo(MOTOR_NG5);
    ui->outBBt_14->setText(QString::number(m_tPlc->m_worker->m_nMOTOR_NG5));
}

void PlcDlg::on_outBBt_15_clicked()
{
    // get OK距离
    m_tPlc->m_worker->readPlcInfo(MOTOR_NG6);
    ui->outBBt_15->setText(QString::number(m_tPlc->m_worker->m_nMOTOR_NG6));
}



void PlcDlg::on_outBBt_20_clicked()
{
    // get 电磁阀吹料时间
    m_tPlc->m_worker->readPlcInfo(BLOW_TIME);
    ui->outBBt_20->setText(QString::number(m_tPlc->m_worker->m_nBLOW_TIME));
}

void PlcDlg::on_confirmBt_15_clicked()
{
    // set 电磁阀吹料时间
    m_tPlc->m_worker->writePlcInfo(BLOW_TIME, ui->inEdit_15->text().toInt());
}

void PlcDlg::on_confirmBt_16_clicked()
{
    // set 吹倒料时间
     m_tPlc->m_worker->writePlcInfo(FALL_TIME, ui->inEdit_16->text().toInt());

}

void PlcDlg::on_confirmBt_17_clicked()
{
    // set 报警时间
     m_tPlc->m_worker->writePlcInfo(WARN_TIME, ui->inEdit_17->text().toInt());

}

void PlcDlg::on_confirmBt_18_clicked()
{
    // set 停机延迟时间
     m_tPlc->m_worker->writePlcInfo(STOP_TIME, ui->inEdit_18->text().toInt());
}

void PlcDlg::on_outBBt_21_clicked()
{
    // get 倒料时间
    m_tPlc->m_worker->readPlcInfo(FALL_TIME);
    ui->outBBt_21->setText(QString::number(m_tPlc->m_worker->m_nFALL_TIME));
}

void PlcDlg::on_outBBt_19_clicked()
{
    // get 报警时间
    m_tPlc->m_worker->readPlcInfo(WARN_TIME);
    ui->outBBt_19->setText(QString::number(m_tPlc->m_worker->m_nWARN_TIME));
}

void PlcDlg::on_outBBt_18_clicked()
{
    // get 停机延迟时间
    m_tPlc->m_worker->readPlcInfo(STOP_TIME);
    ui->outBBt_18->setText(QString::number(m_tPlc->m_worker->m_nSTOP_TIME));
}

void PlcDlg::on_inBT_28_clicked()
{
    // 相机1 触发次数
    if (m_nIndex)
    {
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD1A);
    }
    else{
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD1B);
    }
    m_nIndex = 1 - m_nIndex;
    ui->inBT_28->setText(QString::number(m_tPlc->m_worker->m_nTOUCH_CCD1B * 65536 + m_tPlc->m_worker->m_nTOUCH_CCD1A));
}

void PlcDlg::on_inBT_27_clicked()
{
    // 相机2 触发次数
    if (m_nIndex)
    {
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD2A);
    }
    else{
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD2B);
    }
    m_nIndex = 1 - m_nIndex;
    ui->inBT_27->setText(QString::number(m_tPlc->m_worker->m_nTOUCH_CCD2B * 65536 + m_tPlc->m_worker->m_nTOUCH_CCD2A));
}

void PlcDlg::on_inBT_30_clicked()
{
    // 相机3 触发次数
    if (m_nIndex)
    {
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD3A);
    }
    else{
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD3B);
    }
    m_nIndex = 1 - m_nIndex;
    ui->inBT_30->setText(QString::number(m_tPlc->m_worker->m_nTOUCH_CCD3B * 65536 + m_tPlc->m_worker->m_nTOUCH_CCD3A));
}

void PlcDlg::on_inBT_29_clicked()
{
    // 相机4 触发次数
    if (m_nIndex)
    {
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD4A);
    }
    else{
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD4B);
    }
    m_nIndex = 1 - m_nIndex;
    ui->inBT_29->setText(QString::number(m_tPlc->m_worker->m_nTOUCH_CCD4B * 65536 + m_tPlc->m_worker->m_nTOUCH_CCD4A));
}

void PlcDlg::on_inBT_32_clicked()
{
    // 相机5 触发次数
    if (m_nIndex)
    {
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD5A);
    }
    else{
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD5B);
    }
    m_nIndex = 1 - m_nIndex;
    ui->inBT_32->setText(QString::number(m_tPlc->m_worker->m_nTOUCH_CCD5B * 65536 + m_tPlc->m_worker->m_nTOUCH_CCD5A));
}

void PlcDlg::on_inBT_31_clicked()
{
    // 相机6 触发次数
    if (m_nIndex)
    {
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD6A);
    }
    else{
        m_tPlc->m_worker->readPlcInfo(TOUCH_CCD6B);
    }
    m_nIndex = 1 - m_nIndex;
    ui->inBT_31->setText(QString::number(m_tPlc->m_worker->m_nTOUCH_CCD6B * 65536 + m_tPlc->m_worker->m_nTOUCH_CCD6A));
}

void PlcDlg::on_inBT_33_clicked()
{
    // 编码器 Sum
    if (m_nIndex)
    {
        m_tPlc->m_worker->readPlcInfo(INCODERA);
    }
    else{
        m_tPlc->m_worker->readPlcInfo(INCODERB);
    }
    m_nIndex = 1 - m_nIndex;
    ui->inBT_33->setText(QString::number(m_tPlc->m_worker->m_nINCODERB * 65536 + m_tPlc->m_worker->m_nINCODERA));
}









